<?php
/*
collecting data from various sources


file version 2005-10-19 10:51:18
*/
class grabber {
/*--------------------------------------------------------------------------------------------------
                                                                                         constructor
--------------------------------------------------------------------------------------------------*/
	/*function () {

	}*/
/*--------------------------------------------------------------------------------------------------
                                                                                      public methods
--------------------------------------------------------------------------------------------------*/
	/*
	*/
	function readFile($p=array()) {
		$p=array_merge(array('file'=>NULL,'path'=>$this->filesPath),$p);
		$filename=$p['path'].$p['file'];
		if (!file_exists($filename)) {
			trigger_error("grabber: $filename don't exist.");
			return false;
		}
		return wakFileContent($filename);
	}

	function feedFile($p=array()) {
		$p['file']=isset($p['file'])?$p['file']:NULL;
		$p['path']=isset($p['path'])?$p['path']:$this->filesPath;
		$filename=$p['path'].$p['file'];
		if (!file_exists($filename)) {
			trigger_error("grabber: $filename don't exist.");
			return false;
		}
		$data=wakGetAllVars();
		return wakTemplateFeed($filename,$data);
	}

	function readConstant($p=array()) {
		if (!isset($p['name'])) {
			trigger_error("grabber: 'name' param.not supplied");
			return NULL;
		}
		if (!defined($p['name'])) {
			trigger_error("grabber: {$p['name']} undefined.",E_USER_WARNING);
			return NULL;
		}
		// echo wakaffvar(constant($p['name']),$p['name']);
		return constant($p['name']);
	}

	function readIniFile($p=array()) {
		// getting params or defaults
		$p['path']=isset($p['path'])?$p['path']:'cfg/';
		$p['sections']=isset($p['sections'])?$p['sections']:false;
		$p['autoConstants']=isset($p['autoConstants'])?$p['autoConstants']:false;
		$p['setConstants']=isset($p['setConstants'])?$p['setConstants']:false;
		if (!isset($p['file'])) {
			trigger_error("grabber->readIniFile : file param not provided",E_USER_WARNING);
			return false;
		}
		// searching for file
		$filename=$p['path'].$p['file'];
		if (!file_exists($filename)) {
			trigger_error("grabber->readIniFile : $filename don't exist",E_USER_WARNING);
			return false;
		}
		// parsing file
		$config=parse_ini_file($filename,$p['sections']);

		// setting constants if necessary
		if ($p['autoConstants']) {
			$count=$this->setConstantsFromArray($config,$p['setConstants']);
			//echo "$count <br />".affvar(get_defined_constants(),'constantes',false);
			wakLog("grabber->readIniFile : $count constants set");
		}
		// returning data
		// inserting data into shared vars
		return $config;
	}
/*--------------------------------------------------------------------------------------------------
                                                                                          properties
--------------------------------------------------------------------------------------------------*/
	var $filesPath="xhtml/";
/*--------------------------------------------------------------------------------------------------
                                                                                     private methods
--------------------------------------------------------------------------------------------------*/
	/*
	*/
	/*function () {

	}*/
	/* sets constants from an array for each key being ALL_CAPS_AND_UNDERSCORE
	*/
	function setConstantsFromArray($array,$all=false) {
		$count=0;
		foreach ($array as $key => $value) {
			if (is_array($value)) {
				$count+=$this->setConstantsFromArray($value);
			} else if ((ereg('[A-Z][A-Z_]*[A-Z]',$key) or $all)
			and !defined($key)) {
				define($key,$value);
				$count++;
			}
		}
		return $count;
	}

}
?>
